﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XNA_Game_Engine.GUI
{
    public class GuiSlotList
    {
        public Rectangle ThisObject { get; set; }
        public int Lines { set; get; }
        public int Columns { set; get; }
        public bool IsVisible { set; get; }
        public bool IsFunction { private set; get; }
        public bool IsSelected { set; get; }
        private double KeepClickTime { set; get; }
        //private bool MouseClicked { set; get; }

        public List<GuiSlot> Slots { get; set; }

        //private readonly Rectangle _dragBarrHw = new Rectangle(0, 0, StaticGui.Image[23].Width, StaticGui.Image[23].Height);
        //private Rectangle _dragBarrPosition;
        
        private readonly int _middleWidth = StaticGui.slotsSize;
        private readonly int _middleHeight = StaticGui.slotsSize;

        public GuiSlotList(int positionX, int positionY, int lines, int columns, List<GuiSlot> slots, bool isFunction)
        {
            ThisObject = new Rectangle(positionX, positionY, _middleWidth*columns, _middleHeight*lines);
            Lines = lines;
            Columns = columns;
            IsFunction = isFunction;

            IsVisible = true;

            //_dragBarrPosition = new Rectangle(positionX, positionY-30, 150, 25);

            Slots = new List<GuiSlot>();

            if (slots != null)
                Slots = slots;
            else
                PopulateSlotList(isFunction);
        }

        public void Update(GameTime gameTime)
        {
            if (!IsVisible)
                return;

            for (int i = 0; i < Slots.Count; i++)
                Slots[i].Update(gameTime);

            if (!StaticGui.IsMouseBeingUsed() && StaticGui.AvailableCommands != this && 
                StaticGui.MouseState.LeftButton == ButtonState.Pressed && StaticGui.MouseColision(ThisObject) && 
                StaticGui.CheckTime(gameTime, KeepClickTime))
            {
                Console.WriteLine("Panel selected");

                StaticGui.SelectThisPanel(this);

                //MouseClicked = true;

                KeepClickTime = gameTime.TotalGameTime.TotalMilliseconds;

                //StaticGui.StartDrag(this);
                //StaticGui.UsingMouse(true);
            }

            //if (!MouseClicked)
            //    return;

            //if (StaticGui.MouseState.LeftButton == ButtonState.Pressed)
            //    return;

            //StaticGui.StopDrag();
            //StaticGui.UsingMouse(false);

            //MouseClicked = false;
        }

        public void Draw(SpriteBatch render)
        {
            if (!IsVisible)
                return;

            //render.Draw(IsSelected ? StaticGui.Image[24] : StaticGui.Image[23], _dragBarrPosition, _dragBarrHw,
            //            Color.White);

            for (int i = 0; i < Slots.Count; i++ )
                Slots[i].Draw(render);
        }

        public bool Colision(Rectangle r)
        {
            return ThisObject.Intersects(r);
        }

        public void ClearSlotList()
        {
            PopulateSlotList(false);
        }

        public void PopulateSlotList(bool isFunction)
        {
            Slots.Clear();
            int i = 0;
            if (isFunction)
            {
                i = 1;
                Slots.Add(new GuiSlot(_middleWidth, _middleHeight, 0, 1, StaticGui.Image[21], false, this, null)
                              {
                                  MouseClick = g => Console.WriteLine("value10"),
                                  MouseRightClick = null,
                                  Actions = () =>
                                                {
                                                    StaticGui.TimeStart.Executed = false;
                                                    Console.WriteLine("entrou");
                                                    for (int j = StaticGui.TimeStart.InsideFunctionActionIndex; j<Slots.Count; j++)
                                                    {
                                                        StaticGui.TimeStart.InsideFunctionActionIndex = j;
                                                        if (Slots[j].Actions == null)
                                                        {
                                                            //Console.WriteLine("essa porra null "+j);
                                                            continue;
                                                        }
                                                        if (j == 0)
                                                        {
                                                            //Console.WriteLine("essa porra 0");
                                                            continue;
                                                        }

                                                        if (StaticGui.FunctionSelectedSlot != null)
                                                            StaticGui.FunctionSelectedSlot.IsSelected = false;

                                                        StaticGui.FunctionSelectedSlot = Slots[j];
                                                        StaticGui.FunctionSelectedSlot.IsSelected = true;
                                                        Slots[j].Actions();
                                                        StaticGui.TimeStart.Executed = true;
                                                        break;
                                                    }

                                                    if (StaticGui.TimeStart.InsideFunctionActionIndex + 1 < Slots.Count)
                                                    {

                                                        Console.WriteLine("saiu "+
                                                            StaticGui.TimeStart.InsideFunctionActionIndex + " slot "+
                                                            Slots.Count);
                                                        StaticGui.TimeStart.InsideFunctionActionIndex++;
                                                        StaticGui.TimeStart.ActionIndex--;
                                                    }
                                                    else
                                                    {
                                                        StaticGui.TimeStart.InsideFunctionActionIndex = 0;
                                                        if (StaticGui.FunctionSelectedSlot != null)
                                                            StaticGui.FunctionSelectedSlot.IsSelected = false;
                                                    }
                                                    Console.WriteLine("saiu ");
                                                },
                                  DoDragAction = StaticGui.DragAction
                });
            }
            
            for (; i<Lines; i++)
                for (int n = 0; n < Columns; n++)
                    Slots.Add(new GuiSlot(_middleWidth, _middleHeight, i, n, StaticGui.Image[6], true, this, null));

        }

        public void DragNow(Vector2 keepMouseStateForDragPanel)
        {
            int x = ThisObject.X + (int)keepMouseStateForDragPanel.X;
            int y = ThisObject.Y + (int)keepMouseStateForDragPanel.Y;
            int w = ThisObject.Width;
            int h = ThisObject.Height;

            ThisObject = new Rectangle(x, y, w, h);
            //_dragBarrPosition = new Rectangle(x, y-30, 150, 25);
        }
    }
}
